package com.ming.stringstudy.feedbacksystem.controller;

import com.ming.stringstudy.feedbacksystem.entity.User;
import com.ming.stringstudy.feedbacksystem.pojo.Result;
import com.ming.stringstudy.feedbacksystem.service.IUserService;
import com.ming.stringstudy.feedbacksystem.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User u = userService.login(user);
        if (u == null){
            return Result.error("账号或密码错误");
        }

        Map<String, Object> claims = new HashMap<>();
        claims.put("id", u.getId());
        claims.put("deptId", u.getDeptId());
        claims.put("roleId", u.getRoleId());

        String jwt = JwtUtils.generateJwt(claims);
        return Result.success(jwt);
    }

}
